# @time     :2022/4/14
# @Author   :Simple
# @FileName :handle_excel
from openpyxl import load_workbook, Workbook


def excel2list(filename, sheet_name='Sheet1'):
    """
    把excel表中sheet的内容转换成list
    :param sheet_name:
    :param filename:
    :return:
    """
    wb = load_workbook(filename)
    first_sheet = wb[sheet_name]
    first_row = ""
    for cell in first_sheet["1"]:
        first_row += cell.value + ","
    first_row = first_row[:-1]
    print(first_row)

    # 从第二行开始读取excel
    rows = []
    for row in first_sheet.iter_rows(min_row=2):
        cells = list()
        for cell in row:
            cells.append(cell.value)
        rows.append(tuple(cells))
    return first_row, rows


def list2excel(file_path, contexts):
    """
    把list表格的内容存入excel中
    :param file_path:
    :param contexts:
    :return:
    """
    wb = Workbook()
    ws = wb.active
    for t in contexts:
        ws.append(t)
    wb.save(file_path)


# if __name__ == '__main__':
#     file_name = "../data/test_file.xlsx"
#     print(excel2list(file_name))
    # new_file = "../data/test_file1.xlsx"
    # contexts = [[1, 2, 3], [4, 5, 6]]
    # list2excel(new_file, contexts)
